package com.wpx.Controller;

import com.wpx.Pojo.Article;
import com.wpx.Pojo.ArticleVo;
import com.wpx.Pojo.PageVO;
import com.wpx.Service.ArticleService;
import com.wpx.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/article")
public class ArticleController{
    @Autowired
    public  ArticleService articleService;

    /**
     * 按条件得到所有文章列表
     * @param pageVO
     * @return
     */
    @PostMapping("/getArticleList")
    public Result getArticleList(@RequestBody PageVO pageVO){
         List<Article>l= articleService.getArticleList(pageVO);
        Integer articleTotal = articleService.getArticleTotal(pageVO);
        Long sum=0L;
        if(articleTotal!=null){
            sum=Long.valueOf(articleTotal);
        }
         return Result.ok(l,sum);
    }

    /**
     * 获取总数
     * @param pageVO
     * @return
     */
    @PostMapping("/getArticleTotal")
    public Result getArticleTotal(@RequestBody PageVO pageVO){
        Integer articleTotal = articleService.getArticleTotal(pageVO);
        Long sum=0L;
        if(articleTotal!=null){
            sum=Long.valueOf(articleTotal);
        }
        return Result.ok(null,sum);
    }

    //改变文章的状态
    @GetMapping("/status")
    public Result updateStatus(@RequestParam Integer id ,@RequestParam Integer status){
        articleService.updateStatus(id,status);
         return Result.ok();
    }
    // 添加文章
    @PostMapping("/addArticle")
    public Result addArticle(@RequestBody ArticleVo articleVo){
        System.out.println(articleVo);
        articleService.addArticle(articleVo);

        return Result.ok();
    }

}
